#include <stdio.h>
#include "mazelib.h"
#include "simpio.h"

#define NO_SOLUTION 10000

static int ShortestPathLength(pointT pt);

int main() {
	int length;
	string maze;

	printf("-- Assignment 4A --\n\n");
	printf("Name of the maze: ");
	maze = GetLine();
	ReadMazeMap(maze);
	SetPauseTime(0.05);

	length = ShortestPathLength(GetStartPosition());
	printf("Shortest path: %d\n\n", length);
}

static int ShortestPathLength(pointT pt) {
	directionT dir;
	int pathLength = NO_SOLUTION, tempLength;
	if (OutsideMaze(pt))
		return 0;
	else if (IsMarked(pt))
		return NO_SOLUTION;

	MarkSquare(pt);
	for (dir = North; dir <= West; dir++)
		if ( (!WallExists(pt, dir) && (tempLength = ShortestPathLength(AdjacentPoint(pt, dir)) + 1))
			&& tempLength < pathLength )
				pathLength = tempLength;
	UnmarkSquare(pt);

	return pathLength;
}